<template>
	<view class="app-address-list">
		<view  v-if="dataList.length">
			<view class="address-mess flex-box"
			v-for="(item,index) in dataList" :key="index">
				<u-icon name="edit-pen" size="35" @tap="edit(item)"></u-icon>
				<view class="box-1"  @tap="exchangeAddress(item)" >
					<view class="flex-box">
						<text class="name">{{item.uname}}</text> <text class="tel">{{item.mobile}}</text>
					</view>
					<view class="one_ell">
						<text class="address-in" v-if="item.is_default">默认</text>
						{{item.province+item.city+item.district+item.address}}
					</view>
				</view>
				<!-- <uni-icons type="trash-filled"></uni-icons> -->
				<u-icon name="trash-fill" size="20" @tap="deleteAddress(item.id,index)"></u-icon>

			</view>
		</view>
		<u-empty v-else mode="data" marginTop="100" text="暂无数据"></u-empty>
		<!-- 添加地址 -->
		<view class="add-address">
			<view class="btn" @click="onAddressEdit(0)">
				<u-icon name="plus" color="#fff"></u-icon>
				<text class="mlr">添加收货地址</text>
			</view>
		</view>
	</view>
</template>

<script>
	// import listNone from "@/components/listnone/listnone.vue"
	export default {
		components:{
			
		},
		data() {
			return {
				dataList:[],
				mess:{},
				status:0,
				isNone:false,
			}
		},
		onLoad(option){
			if(option.typeid){
				this.status = 1
			}
		},
		onShow(){
			this.initData()
		},
		methods:{
			/**
			 * 编辑地址点击
			 */
			onAddressEdit(type){
				uni.navigateTo({
					url: '/pages/sub/AddressEdit/AddressEdit?type=' + type,
				})
			},
			deleteAddress(id,index){
				console.log(id);
				uni.showModal({
					title: "删除提醒",
					confirmText:'确定删除',
					confirmColor:'#42b291',
					content:"是否删除该地址",
					success: (res)=> {
						if(res.confirm){
							this.$u.api.addressDel({address_id:id})
							.then(res=>{
								if(res.status){
									this.dataList.splice(index,1)
								}
								uni.showToast({
									icon: 'none',
									title: res.msg
								})
							})
							.catch(err=>{
								console.log(err);
							})
						}
					}
				})
				
			},
			initData(){
				this.$u.api.addressList()
				.then(res=>{
					if(res.status){
						this.dataList = res.msg
						this.isNone = false
						return
					}
					this.isNone = true
				})
				.catch(err=>{
					console.log(err);
					uni.showToast({
						icon: 'none',
						title: "网络异常，请稍后"
					})
				})
			},
			edit(para){
				uni.navigateTo({
					url:'/pages/sub/AddressEdit/AddressEdit?typeb='+2+'&item='+encodeURIComponent(JSON.stringify(para))
				})
			},
			// 修改
			exchangeAddress(item){
					item.is_default = true
					this.$u.api.addressEdit(item)
					.then(res=>{
						// console.log(res)
						if(res.status){
						/*	uni.showToast({
								icon: 'none',
								title: res.msg
							})*/
						/*	setTimeout(()=>{
								uni.navigateBack()
							},500)
							return*/
						}

            setTimeout(()=>{
              uni.navigateBack()
            },500)
						/*uni.showToast({
							icon: 'none',
							title: res.msg
						})*/
					})
					.catch(err=>{
						console.log(err);
						uni.showToast({
							icon: 'none',
							title: "网络异常，请稍后再试"
						})
					})
			},
			returnBack(para){
				uni.$emit('changeaddress',{msg:encodeURIComponent(JSON.stringify(para))})
				uni.navigateBack()
				if(this.status==1){
					
				}
			}
		},
		onUnload(){
			uni.$off();
		},
		
		
	}
</script>

<style scoped lang="scss">
	@import 'AddressList.scss';
</style>
